home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / mymenv-notify 2.0 / myAEvents.h < prev    next >
Text File  |  1995-08-10  |  4KB  |  113 lines

  1. /*
  2.  ************************************************************************
  3.  *         Handling of Mandatory Apple Events
  4.  *        OpenApplication, {Open|Print}Document, Quit
  5.  */
  6.  
  7.  
  8. class AppleEventGenericHandler
  9. {
  10.   const int signature;
  11.   enum { Signature_value = 57049 };
  12.  
  13.   AppleEvent * theAppleEvent;            // The event we got
  14.   AppleEvent * reply;                // Descriptor to add replies to
  15.   virtual OSErr operator() (void) = 0;        // This is the real handler
  16.   AEEventHandlerUPP zero_level_handler_descr;    // Just to tell the system if it's a native handler
  17.   static pascal OSErr zero_level_handler(AppleEvent *theAppleEvent, 
  18.                      AppleEvent *reply, long handlerRefcon);
  19. protected:
  20.   const AEEventClass event_class;  
  21.   const AEEventID event_id;            // Event we're handling
  22.  
  23.   Boolean got_all_params(void);            // Check to make sure we got all required params
  24.   void get_direct_list(AEDescList& list_desc);    // Get Descriptor of a list as a direct param
  25.  
  26. public:
  27.   AppleEventGenericHandler(const AEEventID _event_id);    // Install the handler
  28.   ~AppleEventGenericHandler(void);            // Uninstalls the handler
  29. };
  30.  
  31. class AppleEventOpenAppl : public AppleEventGenericHandler
  32. {
  33.  public:
  34.   AppleEventOpenAppl(void) : AppleEventGenericHandler(kAEOpenApplication) {}
  35. };
  36.  
  37. class QuitFlag : public AppleEventGenericHandler
  38. {
  39.   Boolean quit_flag;
  40.   virtual OSErr operator() (void)  { quit_flag = TRUE; return noErr; }
  41.  public:
  42.   QuitFlag(void) : AppleEventGenericHandler(kAEQuitApplication), quit_flag(FALSE) {}
  43.   Boolean quitting(void) const    { return quit_flag; }
  44. };
  45.  
  46.             // Get a full path from an alias record
  47.             // Returns a pointer to a static string
  48. const char * get_full_path(AliasHandle alias_handle);
  49.  
  50.                 // Handle an OpenDocument AEvent with the help
  51.                 // of a plug-in class DocHandler
  52.                 //   class DocHandler {
  53.                 //   public: DocHandler(const int no_docs_to_open);
  54.                 //        // throw up if it doesn't like this
  55.                 //        // this number
  56.                 //   Boolean operator () (AliasHandle doc_handle);
  57.                 //        // Open the named document. Return
  58.                 //        // FALSE if doesn't want to handle
  59.                 //        // the rest 
  60. template <class DocHandler>
  61. class AppleEventOpenDoc : public AppleEventGenericHandler
  62. {
  63.   AEDescList desc_opened_doc;            // Decsriptor of a list of open doc
  64.   long no_opened_doc;                    // Number of documents to open
  65.  
  66.   virtual OSErr operator() (void)    
  67.                   // This is the real handler
  68.                   // Sorry, this has to be inline
  69.                   // (CW6 sucks a bit on templates)
  70. {
  71.   get_direct_list(desc_opened_doc);
  72.   assert( got_all_params() );
  73.   do_well( AECountItems(&desc_opened_doc, &no_opened_doc) );
  74.   OSErr err_sofar = noErr;
  75.   DocHandler doc_handler(no_opened_doc);
  76.   int want_handle = 1;                // Should've asked DocHandler abou that
  77.   assert( want_handle <= no_opened_doc );
  78.   
  79.   const int alias_record_prealloc = 800;    // To preallocate storage to copy
  80.                           // an alias record to (from AEDescr)
  81.                           // Hope that'll be enough
  82.   AliasHandle alias_handle = (AliasHandle)NewHandle(alias_record_prealloc);
  83.   for(register int index=1; err_sofar == noErr && index <= want_handle; index++)
  84.   {
  85.     Size actual_size;
  86.     AEKeyword keyword;
  87.     DescType type_code;
  88.     
  89.     HLock((Handle)alias_handle);
  90.     do_well( AEGetNthPtr(&desc_opened_doc, index, typeAlias, &keyword, &type_code,
  91.                     (Ptr)*alias_handle, alias_record_prealloc, &actual_size) );
  92.     assert( actual_size < alias_record_prealloc );    // Make sure we got the whole
  93.     assert( actual_size == (**alias_handle).aliasSize );    // record
  94.     HUnlock((Handle)alias_handle);
  95.     
  96.                         // Note, returned alias is a minimal alias
  97.     FSSpec resolved_target;        // we make it a full one (via UpdateAlias)
  98.     Boolean was_changed;
  99.     do_well( ResolveAlias(0,alias_handle,&resolved_target,&was_changed) );
  100.     do_well( UpdateAlias(0,&resolved_target,alias_handle,&was_changed) );
  101.     if( !doc_handler(alias_handle) )
  102.       err_sofar = errAEEventNotHandled;
  103.   }    
  104.   do_well( AEDisposeDesc(&desc_opened_doc) );
  105.   DisposeHandle((Handle)alias_handle);
  106.   
  107.   return err_sofar;
  108. }
  109.  public:
  110.   AppleEventOpenDoc(void) : AppleEventGenericHandler(kAEOpenDocuments) {}
  111. };
  112.  
  113.